/**********************************************************************************
 * Copyright (c) 2012 - 2012,  parallel, All right reserved
 * Title: com.adc.parallel.constants  WaitList.java 
 * @author shen  shenqiao2012@163.com 
 *  2012-11-21 下午4:12:15 
 *********************************************************************************/


package com.adc.parallel.constants;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;




@SuppressWarnings({"unchecked","rawtypes"})
public class WaitList {
	/**
	 * task wait list
	 */
	private static List waitList;
	
	/**
	 * @return the waitList
	 */
	public synchronized static List getWaitList() {
		if (waitList==null) {
			waitList=Collections.synchronizedList(new ArrayList());
		}
		return waitList;
	}
	
	/**
	 * 
	 * Description: define list type
	 * author:  shen
	 * @return String
	 */
	public static String getType() {
		return SysConstants.ListType_WT;
	}
	public synchronized static void addTaskDto(Object taskDto) {
		getWaitList().add(taskDto);
	}
	
	/**
	 * 
	 * Description: get the first task dto
	 * author:  shen
	 * @return E
	 */
	public synchronized static Object getFirstTaskDto() {
		Object taskDto = null;
		if (waitList.size()>0) {
			taskDto = getWaitList().get(0);
			waitList.remove(0);
		}
		return taskDto;
	}
	
	/**
	 * 
	 * Description: insert timeout taskDto 
	 * author:  shen
	 * @param taskDto 
	 * @return void
	 */
	public synchronized static void insertBrokenTaskDto(Object taskDto){
		int size=waitList.size();
		int index=size/2;
		if (size>1) {
			index=index+1;
		}
		getWaitList().add(index, taskDto);
	}

	/**
	 * 
	 * Description: to clear persisted dto
	 * author:  shen
	 * @param list 
	 * @return void
	 */
	public static void removeAllDto(final List list) {
		waitList.removeAll(list);
	}

}
